package cn.com.hhrcw.validator;

import cn.com.hhrcw.entity.CvResume;
import cn.com.hhrcw.entity.OpJob;
import cn.com.hhrcw.params.OpDeliveryHistoryParams;
import cn.com.hhrcw.service.ICvResumeService;
import cn.com.hhrcw.service.IOpJobService;
import cn.com.hhrcw.validator.constraints.ResumeRequired;
import cn.com.hhrcw.validator.resume.util.ResumeRequiredValidationUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author leeson
 */
@Slf4j
public class ResumeRequiredValidation implements ConstraintValidator<ResumeRequired, OpDeliveryHistoryParams> {

    @Autowired
    private ICvResumeService cvResumeService;

    @Autowired
    private IOpJobService jobService;

    @Autowired
    private ResumeRequiredValidationUtil resumeRequiredValidationUtil;

    @Override
    public boolean isValid(OpDeliveryHistoryParams obj, ConstraintValidatorContext context) {
        context.disableDefaultConstraintViolation();
        if (StrUtil.isBlank(obj.getR10()) || StrUtil.isBlank(obj.getJobId())) {

            log.warn("请选择投递职位,obj.getR10():{},obj.getJobId():{}", obj.getR10(), obj.getJobId());
            context.buildConstraintViolationWithTemplate("请选择投递职位").addConstraintViolation();
            return false;
        }
        CvResume resume = cvResumeService.getById(obj.getResumeId());
        //附件简历不验证
        if (resume.getType() == 1) {
            return true;
        }
        OpJob opJob = jobService.getById(obj.getJobId());
        if (opJob == null || opJob.getDelFlag()) {
            context.buildConstraintViolationWithTemplate("该职位已删除，暂不能投递！").addConstraintViolation();
            return false;
        }
        if (opJob.getRecruitmentState() == 1) {
            context.buildConstraintViolationWithTemplate("该职位已停止招聘，暂不能投递！").addConstraintViolation();
            return false;
        }
        if (resume.getType() == 0) {
            boolean isSchool = resume.getResumeType() == 2;
            if (isSchool && opJob.getRecruitType() != 1) {
                context.buildConstraintViolationWithTemplate("校招简历只能投递校招职位").addConstraintViolation();
                return false;
            } else if (resume.getResumeType() == 1 && opJob.getRecruitType() != 0) {
                context.buildConstraintViolationWithTemplate("社招简历只能投递社招职位").addConstraintViolation();
                return false;
            }
        }
        //手机端传递 R10 为-1  判断是否为手机端和PC端
        if("-1".equals(obj.getR10())){
            obj.setR10("1");//手机端 默认同意调剂
            return true;
        }else{
            Result r = resumeRequiredValidationUtil.isValid(obj.getResumeId(), true);
            if (r.isSuccess()) {
                return true;
            } else {
                context.buildConstraintViolationWithTemplate(r.getMessage()).addConstraintViolation();
                return false;
            }
        }
    }
}
